package default_package;

import java.io.PrintWriter;
import java.util.TreeMap;

public class ThreadUsersConnected implements Runnable {
	private Controleur c;
	private PrintWriter out;

	public ThreadUsersConnected(Controleur c, PrintWriter out) {
		super();
		this.c = c;
		this.out = out;
		
	}

	@Override
	public void run() {
	//	boolean diff = false;
		try
		{
			while(true)
			{
				out.println("%Server% DebEnvoisInfos");
				out.flush();
				TreeMap<String, Utilisateur> users = new TreeMap<String, Utilisateur>();
				for(Utilisateur u : c.getUsersConnected().values()) {
					users.put(u.getLogin().toLowerCase(), u.clone());
				}
				
				for(Utilisateur user : users.values()) {
						out.println(user.getLogin());
						out.flush();
						out.println(user.getNom()); 
						out.flush();
						out.println(user.getPrenom());
						out.flush();
						out.println(user.getSexe());
						out.flush();
						out.println(user.getStatut());
						out.flush();
						out.println(user.getLangueMaternelle());
						out.flush();
				}
				boolean diff = false;
				while(!diff) {
					try {
						Thread.sleep(1000);
						TreeMap<String, Utilisateur> users2 = c.getUsersConnected();
						if(users.size() == users2.size()) {
							for(String login : users.keySet()) {
								if(!users.get(login).getStatut().equals(users2.get(login).getStatut())) {
									diff = true;
								}
							}
						} else {
							diff = true;
						}
					} catch (InterruptedException e) {
						Thread.currentThread().interrupt();
						throw new InterruptedException();
					}
				}
				
			}
		}
		catch(InterruptedException e)
		{
			out.println("%Server% FinEnvoisInfos");
			out.flush();
		}
	}

	
}